package com.cjq.dao.Impl;

import com.cjq.bean.Courier;
import com.cjq.bean.User;
import com.cjq.dao.BaseUserDao;
import com.cjq.util.DBUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDaoMysql extends DBUtils implements BaseUserDao {
    public static final String sql_user_console = "select count(id) data1_size,count(TO_DAYS(createtime)=TO_DAYS(NOW()) or null) data1_day from user";

    public static final String sql_user_findAll = "select * from user";
    public static final String sql_user_findAll_limit = "select * from user limit ?,?";

    public static final String sql_user_insert = "insert into user (username,userphone,password,createtime,logintime) values(?,?,?,NOW(),NOW())";

    public static final String sql_user_findByUserPhone = "select * from user where userphone = ?";

    public static final String sql_user_updae = "update user set username = ?,userphone=?,password=? where id = ?";

    public static final String sql_user_delete = "delete from user where id = ?";

    public static final String sql_user_updateLoginTime = "update user set logintime = NOW() where userphone = ?";

    public static final String sql_user_insertByPhone = "insert into user (userphone,createtime,logintime) values(?,NOW(),NOW())";


    @Override
    public List<Map<String, Integer>> console() {
        List<Map<String, Integer>> data = new ArrayList<>();
        try {
            ResultSet rs = query(sql_user_console, null);
            if (rs.next()) {
                int data1_size = rs.getInt("data1_size");
                int data1_day = rs.getInt("data1_day");
                Map data1 = new HashMap();

                data1.put("data1_size", data1_size);

                data1.put("data1_day", data1_day);

                data.add(data1);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeall();
        }

        return data;
    }

    @Override
    public List<User> findAll(boolean limit, int offset, int pageNumber) {
        List<User> data = new ArrayList<>();
        ResultSet rs = null;

        try {
            if (limit) {   //limit为true表示为true   表示分页查询
                List params = new ArrayList();
                params.add(offset);
                params.add(pageNumber);
                rs = query(sql_user_findAll_limit, params);
            } else {
                rs = query(sql_user_findAll, null);
            }

            ResultSetMetaData metaData = rs.getMetaData();     //存储结果集信息
            int columnCount = metaData.getColumnCount(); //得到列数
            String[] columnNames = new String[columnCount];  //存放结果的列名
            for (int i = 0; i < columnCount; i++) {
                columnNames[i] = metaData.getColumnName(i + 1);
            }

            //得到实体类中的所有方法  获取bean中的Student类的所有方法
            Method[] methods = User.class.getDeclaredMethods();
            while (rs.next()) {

                User user = new User();
                for (String columnname : columnNames) {
                    String name = "set" + columnname;
                    for (Method method : methods) {
                        if (method.getName().equalsIgnoreCase(name)) {

                            method.invoke(user, rs.getObject(columnname));//执行了对应的set方法
                            break;
                        }
                    }
                }
                data.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }

        return data;
    }

    @Override
    public boolean insert(User user) {

        try {
            List params = new ArrayList();
            params.add(user.getUserName());
            params.add(user.getUserPhone());
            params.add(user.getPassword());

            int update = update(sql_user_insert, params);

            if (update > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return false;
    }

    @Override
    public User findByUserPhone(String userPhone) {
        User user = null;
        try {
            List params = new ArrayList();
            params.add(userPhone);
            ResultSet rs = query(sql_user_findByUserPhone, params);

            ResultSetMetaData metaData = rs.getMetaData();     //存储结果集信息
            int columnCount = metaData.getColumnCount(); //得到列数
            String[] columnNames = new String[columnCount];  //存放结果的列名
            for (int i = 0; i < columnCount; i++) {
                columnNames[i] = metaData.getColumnName(i + 1);
            }

            //得到实体类中的所有方法  获取bean中的Student类的所有方法
            Method[] methods = User.class.getDeclaredMethods();
            while (rs.next()) {
                user = new User();
                for (String columnname : columnNames) {
                    String name = "set" + columnname;
                    for (Method method : methods) {
                        if (method.getName().equalsIgnoreCase(name)) {
                            method.invoke(user, rs.getObject(columnname));//执行了对应的set方法
                            break;
                        }
                    }
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }

        return user;
    }


    @Override
    public boolean update(User user) {

        try {
            List params = new ArrayList();
            params.add(user.getUserName());
            params.add(user.getUserPhone());
            params.add(user.getPassword());
            params.add(user.getId());
            return update(sql_user_updae, params) > 0 ? true : false;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return false;
    }

    @Override
    public boolean delete(int id) {
        try {
            List params = new ArrayList();
            params.add(id);
            return update(sql_user_delete, params) > 0 ? true : false;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }

        return false;
    }


    @Override
    public boolean updateLoginTime(String phone) {
        try {
            List params = new ArrayList();
            params.add(phone);
            return update(sql_user_updateLoginTime, params) > 0 ? true : false;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }

        return false;
    }

    @Override
    public boolean insertByPhone(String phone) {
        try {
            List params = new ArrayList();
            params.add(phone);
            return update(sql_user_insertByPhone, params) > 0 ? true : false;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return false;
    }
}
